package xyz.yolin.service;

import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.yolin.mapper.QuestionMapper;
import xyz.yolin.mapper.QuestionOptMapper;
import xyz.yolin.pojo.Question;
import xyz.yolin.pojo.QuestionOpt;
import xyz.yolin.utils.BeanMapUtils;
import xyz.yolin.utils.MapParameter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements QuestionService{
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionOptMapper questionOptMapper;

    @Override
    public int create(Question question) {
        int flag=0;
        if (question.getId()!=null){
            flag=this.update(question);
            System.out.println("questionid:"+question.getId());
            questionOptMapper.delete(MapParameter.getInstance().put("questionId",question.getId()).getMap());
        }else {
            flag=questionMapper.create(question);
        }


        if (flag>0){
            List<QuestionOpt> options = question.getOptions();
            int i=0;
            for(QuestionOpt option:options){
                option.setSurveyId(question.getSurveyId());
                option.setQuestionId(question.getId());
                option.setOrderby(++i);
                questionOptMapper.create(option);
            }
        }

        return question.getId();
    }

    @Override
    public int delete(String str) {
        List<String> list = Splitter.on(",").splitToList(str);
        int flag=0;
        for(String i:list){
            questionMapper.delete(MapParameter.getInstance().putId(Integer.parseInt(i)).getMap());
            questionOptMapper.delete(MapParameter.getInstance().put("questionId",Integer.parseInt(i)).getMap());
            flag++;
        }
        return flag;
    }

    @Override
    public int update(Question question) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(question))
                .putId(question.getId()).getMap();
        return questionMapper.update(map);
    }

    @Override
    public List<Question> query(Question question) {
        //仅仅查出问题
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(question)).getMap();
        List<Question> questionList = questionMapper.query(map);
        //还要查出选项
        List<QuestionOpt> optList=questionOptMapper.query(MapParameter.getInstance().put("surveyId",question.getSurveyId()).getMap());
        //嵌套循环遍历出问题和选项
        for(Question question1:questionList){
            List<QuestionOpt> options=new ArrayList<>();
            for(QuestionOpt questionOpt:optList){
                if (question1.getId()==questionOpt.getQuestionId()){
                    options.add(questionOpt);
                }
            }
            question1.setOptions(options);
        }
        return questionList;
    }

    @Override
    public Question detail(int id) {
        Map<String, Object> map = MapParameter.getInstance().putId(id).getMap();
        return questionMapper.detail(map);
    }

    @Override
    public int count(Question question) {
        return questionMapper.count(MapParameter.getInstance().put(BeanMapUtils.beanToMap(question)).getMap());
    }
}
